import web
import MFRC522
import subprocess
import signal
import os

urls = (
    '/', 'index'
)

class index:
	def __init__(self):
		global process
	def GET(self):
		tagUId_old = ''
		tagUId = ''
		MIFAREReader = MFRC522.MFRC522()
 
		while tagUId_old == tagUId: 
			(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
			if status == MIFAREReader.MI_OK:
				(status,backData) = MIFAREReader.MFRC522_Anticoll()
				if status == MIFAREReader.MI_OK:
					tagUId = str(backData[0])+"@"+str(backData[1])+"@"+str(backData[2])+"@"+str(backData[3])+"@"+str(backData[4])
		MFRC522.MFRC522_cleanUp
		return tagUId;
	def POST(self):
		global process
		input = web.input(command="nothing")
		if(input.command == 'start'):
			process = subprocess.Popen('python /home/pi/CompetitionSuite/runGame.py', shell=True ,preexec_fn=os.setsid);
		elif(input.command == 'stop'):
			os.killpg(process.pid, signal.SIGTERM)
		return'success'
if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()